package com.sogoodsoft.web.crm;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.crm.OrderForm;
import com.sogoodsoft.service.common.AbstractService;
import com.sogoodsoft.service.crm.OrderService;
import com.sogoodsoft.util.DwzMsg;
import com.sogoodsoft.util.InfosysUtils;
import com.sogoodsoft.web.BaseAction;

@SuppressWarnings("serial")
public class OrderConfirmAction extends BaseAction<OrderForm, Long> {

	@Autowired
	private OrderService service;
	
	@SuppressWarnings("rawtypes")
    @Override
	protected AbstractService getService() {
		return service;
	}
	
	
	@SuppressWarnings("unchecked")
	public String list() throws Exception{
		  List < PropertyFilter > filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		  
		  //默认查询未确认过的订单
		  	if(filters.size()==0){
		  		PropertyFilter p = new PropertyFilter("EQL_statusid", String.valueOf(OrderForm.STATUS.UNCONFIRMED.getValue()));
		  		filters.add(p);
		  		
		  	}
	        // 设置默认排序方式
	        if (!page.isOrderBySetted()) {
	            page.setOrderBy(orderby);
	            page.setOrder(order);
	        }

	        logger.debug("查询列表");
	        page = getService().findPage(page, filters);
	        return SUCCESS;
	}
	
	/**
	 * 订单确认
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		DwzMsg dwz = new DwzMsg();
		dwz.message = "订单编号："+entity.getCode()+" 金额："+entity.getTotalcash()+" 确认成功！";
		dwz.statusCode = DwzMsg.STATUS.SUCCESS;
		dwz.navTabId = Struts2Utils.getParameter(DwzMsg.param_navTabId);
		dwz.callbackType = DwzMsg.closeCurrent;
		entity.setStatusid(OrderForm.STATUS.CONFIRMED.getValue());
		service.confirm(entity);
		Struts2Utils.renderJson(dwz.msg());
		return null;
	}
	
	/**
	 * 按订单编号载入订单
	 * 
	 * @return
	 * @throws Exception
	 */
	public String input() throws Exception {
		if (entity != null) {
			if (entity.getConfirm() != null) {

				Struts2Utils.renderJson(DwzMsg.fail("对不起，此订单已确认完成！<br>确认人："
						+ entity.getConfirm() + "，时间："
						+ InfosysUtils.dateFormat(entity.getConfirmdate())));
				return null;
			}
			if (entity.getConfirmdate() == null) {
				entity.setConfirmdate(new Date());
			}
		}
		return INPUT;
	}


}
